
<template>
  <el-dialog title="新建主体单位"
             v-el-drag-dialog
             :visible.sync="formVisible"
             :close-on-click-modal="false"
             center
             width="60%"
             :before-close="handleClose"
             >
    <form-assembly
      ref="formAssembly"
      :entryFrom="entryFrom"
      :formData="dynFormData.formData"
      :mainSubTable="dynFormData.mainSubTable"
      @makeSure="doCreate"
      @cancel="closeDialog"
    >
    </form-assembly>
  </el-dialog>
</template>


<script>
import principalPartApi from '@/api/platformLiyonghui/principalPart'
import FormAssembly from '@/components/FormAssembly'
import specialAttr from '@/utils/special-attr'
import commonApi from '@/api/platformLiyonghui/common'
var codeRules = {};
import elDragDialog from '@/directive/el-drag-dialog'

export default {
  name: 'PrincipalPartAdd',
  directives: { elDragDialog },
  components: {
    FormAssembly,
  },
  data() {
    return {
      parentId: '0',
      // 动态表单
      dynFormData:{
        formData:{}
      },
      entryFrom: 'add',//入口，add(新增),edit(编辑)，判断是从‘新增’进入，还是从‘编辑’进入，必填
 options: {
   parentCode: '',
},
      formVisible: false,
    }
  },
 async created() {
  },
  methods: {
    /**
     * 打开新建表单
     */
    handleCreate() {
      // const objData = JSON.parse(JSON.stringify(this.$store.getters.dynamicList[this.$route.meta.id][0]));
      const objDatas = [];
      const code = sessionStorage.getItem('routerCode');
      const menus = JSON.parse(sessionStorage.getItem('routerMenus'));
      console.log(code)
      console.log(menus)
      if(menus!=null){
        menus.forEach((item) => {
          if(item.menuCode==code){
            objDatas.push(item)
          }
        })
      }
      const objData = objDatas[0]
      //获取字典项
      const dictionaryEntry = JSON.parse(sessionStorage.getItem('constCode'))
      const applicationEntry = JSON.parse(localStorage.getItem('applications'))
      let applicationCode;
      for (let i = 0; i < applicationEntry.length; i++) {
        let item = applicationEntry[i];
        if (objData.applicationId === item.id) {
          applicationCode = item.code;
          break;
        }
      }
      objData.formData.formList.forEach(item => {
        item.prop = item.prop.replace('.', '$')
        // 组装枚举数据
        if(item.fieldSpecialType === specialAttr.fieldSpecialType.ENUM_TYPE.code){
          // item.options = this.$store.getters.dictList[item.dicTypeData].map(item => ({ label: item.name, value: item.value}))
          item.options = []
          dictionaryEntry.forEach((item1) => {
            if(item1.constCode == item.dicTypeData){
              item.options.push({ label: item1.name, value: item1.value })
            }
          })
        // 组装外键数据
        }else if(item.fieldSpecialType === specialAttr.fieldSpecialType.FK_TYPE.code){
          item.options = this.options[item.foreignData]
        // 组装多对多实体数据
        }else if(item.fieldSpecialType === specialAttr.fieldSpecialType.MTM_TYPE.code){
          item.options = this[item.webEntityData]
        // 组装其他数据
        }else if(item.fieldSpecialType === specialAttr.fieldSpecialType.OTHER_WEB.code){
          item.options = this[item.webEntityData]
        }
        //编码规则校验
        if(item.isSerialNumber==true){
          this.form={
            groupId: localStorage.getItem('SET_GROUP_ID'),
            orgId: localStorage.getItem('SET_ORG_ID'),
            tableColumn: objData.tableName + '_' + item.column,
            applicationCode: applicationCode,
          }
          item.value = '';
           commonApi.number(this.form)
            .then(res => {
              console.log(res.data)
              if (res.data !== null&& res.data.readonly ===true) {
                var ddd = {};
                  item.value = res.data.retCode;
                  item.disabled = true
                  item.isCustomValue = true
                  codeRules[item.prop] = res.data.id
              }
            })
        }
      })
      this.dynFormData = objData
      this.formVisible = true
    },
    /**
     * 执行新建操作
     */
    doCreate(form) {
      form.parentId = this.parentId;
      form.dbCodeIdMap = codeRules;
      principalPartApi.insert(form)
        .then(res => {
          if (res.code === '000000') {
            this.$message({
              type: 'success',
              message: '保存成功!'
            })
            this.$emit('created')
            this.$refs['formAssembly'].cancelForm()
            this.formVisible = false
          }else{
            this.$message({
            type: 'error',
            message: res.message
           })
         }
        })
    },
    // 子组件点击取消按钮让dialog的formVisible:false;
    closeDialog(){
      this.formVisible = false;
    },
    // 点击右上角的关闭按钮时让form表单置空
    handleClose(done){
      this.$refs['formAssembly'].cancelForm();
      done();
    }
  }
}
</script>
